package org.ank.food;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class LaunchActivity extends Activity{
	
	private TextView title, copyTextView;
	private Button /*viewManButton,*/ helpButton, startButton;
	private ProgressBar progressBar;
	
	@Override
	public void onCreate(Bundle b){
		boolean dbFileExists = true;
		super.onCreate(b);
		setContentView(R.layout.launch_activity);
		
		helpButton = (Button)findViewById(R.id.launch_help_button);
		startButton = (Button)findViewById(R.id.launch_start_button);
		title = (TextView)findViewById(R.id.launch_title);
		progressBar = (ProgressBar)findViewById(R.id.launch_progress_bar);
		copyTextView = (TextView)findViewById(R.id.launch_copying_textview);
		
		

        //just check if the database file exists, if not - copy it from /res/raw
        try{
        	FileInputStream file = new FileInputStream(ProductDataProvider.DATABASE_LOCATION+
        			ProductDataProvider.DATABASE_NAME);
        	int c;
//        	while( (c = file.read()) != -1 );
//        	Log.v("launch", String.valueOf(c)); 
        	file.close();
        }catch(FileNotFoundException e){
//        	e.printStackTrace();
//        	Log.v("launch","file not found - fnfe");
        	dbFileExists = false;
        	copyDatabase();
        } catch (IOException e) {
//			e.printStackTrace();
//			Log.v("launch","file not found - ioe");
			dbFileExists = false;
			copyDatabase();
		}
        if (dbFileExists){
        	Intent intent = new Intent();
        	intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
//        	Log.v("launch","file found");
        	startActivity(intent);
        	finish();
        }
	}
	private void copyDatabase(){
		
		title.setText(getString(R.string.launch_text)+"\n"
				+getString(R.string.app_name)+" "+getString(R.string.app_version));
		DBCopyingHandler2 handler = new DBCopyingHandler2();
	    Thread copyThread = new Thread(new DBCopier(getApplicationContext(),handler));
	    copyThread.start();
	}
	
	public class DBCopyingHandler2 extends Handler{
		
		@Override
		public void handleMessage(Message m){
			switch (m.what){
			case DBCopier.DB_COPY_FAIL_CODE :
				break;
			case DBCopier.DB_COPY_BEGIN_CODE :
				progressBar.setVisibility(View.VISIBLE);
				copyTextView.setVisibility(View.VISIBLE);
				break;
			case DBCopier.DB_COPY_SUCCESS_CODE :
				progressBar.setVisibility(View.GONE);
				copyTextView.setVisibility(View.GONE);
				helpButton.setOnClickListener(new Button.OnClickListener(){
					@Override
					public void onClick(View v) {
						Intent intent = new Intent();
			    		intent.setClass(getApplicationContext(), InfoActivity.class);
			    		startActivity(intent);
			    		finish();						
					}
				});
				startButton.setOnClickListener(new Button.OnClickListener(){
					@Override
					public void onClick(View v) {
						Intent intent = new Intent();
			    		intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
			    		startActivity(intent);
			    		finish();						
					}
				});
				break;
			}
			
		}
	}
}
